package com.hq.fxh.No5_User.MyModle;

import android.content.Context;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.EditText;

import com.hq.fxh.R;


/**
 * 可清空的输入框
 * Created by 大圣  on 2015/7/29 0029.
 */
public class ClearableEidtText extends EditText {
    private Drawable dRight;
    private Drawable dLeft;
    private Drawable dTop;
    private Drawable dButtom;
    private Context context;

    /**
     * @param context
     */
    public ClearableEidtText(Context context) {
        super(context);
        this.context = context;
        init();

    }

    public ClearableEidtText(Context paramContext, AttributeSet paramAttributeSet) {
        super(paramContext, paramAttributeSet);
        this.context = paramContext;
        init();
    }

    public ClearableEidtText(Context paramContext, AttributeSet paramAttributeSet, int paramInt) {
        super(paramContext, paramAttributeSet, paramInt);
        this.context = paramContext;
        init();
    }

    /**
     * 初始化
     */
    private void init() {
        dRight = context.getResources().getDrawable(R.mipmap.ic_close_pressed);
        dRight.setBounds(0, 0, dRight.getIntrinsicWidth(), dRight.getIntrinsicHeight());
//        setPadding(6, 6, 10, 6);
        addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                ClearableEidtText.this.setEditTextDrawable();
            }
        });
    }

    /**
     * 控制图片的显示
     */
    private void setEditTextDrawable() {
        if (getText().toString().length() == 0) {
            setCompoundDrawables(dLeft, dTop, null, dButtom);
        } else {
            setCompoundDrawables(dLeft, dTop, dRight, dButtom);
        }
    }

    /**
     * @see android.widget.TextView#onDetachedFromWindow()
     */
    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        dRight = null;

    }

    /**
     * @see android.widget.TextView#setCompoundDrawables(Drawable, Drawable, Drawable, Drawable)
     */
    @Override
    public void setCompoundDrawables(Drawable left, Drawable top, Drawable right, Drawable bottom) {
        if(right != null) {
            dRight = right;
        }
        dLeft = left;
        dTop = top;
        dButtom = bottom;
        super.setCompoundDrawables(left, top, right, bottom);
    }

    /**
     * 添加触摸事件 点击之后 出现 清空editText的效果
     * @see android.widget.TextView#onTouchEvent(MotionEvent)
     */
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if ((this.dRight != null) && (event.getAction() == 1)) {
            Rect rBounds = this.dRight.getBounds();
            int i = (int) event.getX();// 距离屏幕的距离
            int j = (int) event.getY();
            if (i > getWidth() - rBounds.width()- getPaddingRight() && i <= getWidth() && j >= 0
                    && j <= getHeight()) {
                setText("");
                event.setAction(MotionEvent.ACTION_CANCEL);
            }
        }
        return super.onTouchEvent(event);
    }
}
